package com.kalix.jyksh.record.biz;

import com.kalix.framework.core.impl.biz.GenericBizServiceImpl;
import com.kalix.framework.core.util.ConfigUtil;
import com.kalix.ksh.proint.api.model.InfUtil;
import com.kalix.ksh.record.api.biz.IWarningListRecordBeanService;
import com.kalix.ksh.record.api.dao.IWarningListRecordBeanDao;
import com.kalix.jyksh.record.entities.WarningListRecordBean;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 *
 * 功能描述: 根据告警信息列表需要数据格式，返回数据
 *
 * @auther: Whw
 * @date: 2019/1/14 0014 17:13
 * @param:
 * @return:
 */

public class WarningListRecordServiceImpl extends GenericBizServiceImpl<IWarningListRecordBeanDao, WarningListRecordBean> implements IWarningListRecordBeanService {
    public WarningListRecordServiceImpl(){
        super.init(WarningListRecordBean.class.getName());
    }
    public static String wsIp = (String) ConfigUtil.getConfigProp("FREEBOARD_IP","config.ksh.portconf");
    public static String wsPort =(String)ConfigUtil.getConfigProp("FREEBOARD_PORT","config.ksh.portconf");
    /**
     * @Description 获取告警信息数据
     * @auther: 
     * @date: 2018/10/30 0030 下午 4:56
     */
    public String getWarningValue() {
        String url = "http://"+wsIp+":"+wsPort+"/query/mo/AlarmEvent?where=(alarmStatus=2%20or%20alarmStatus=3)&order=alarmTime%20desc";
        String da= InfUtil.interfaceUtil(url,"");
        JSONArray array  = new JSONArray(da);
        JSONArray data = new JSONArray();
        for (int i = 0; i < array.length(); i++) {
            JSONObject ob  = (JSONObject) array.get(i);
            String name = ob.getString("name");// 获取告警信息设备名称
            String levelValue = "";
            JSONArray properties = ob.getJSONArray("properties");
            String level = getLevel(properties);
            if ("1".equals(level)){
                levelValue = "提示";
            }else if("2".equals(level)) {
                levelValue = "低级";
            }else if("3".equals(level)) {
                levelValue = "中级";
            }else if("4".equals(level)) {
                levelValue = "高级";
            }else if("5".equals(level)) {
                levelValue = "紧急";
            }
            String alarmContext = getAlarmContext(properties);
            String topMoType = getTopMoType(properties);
            JSONArray obj = new JSONArray();
//            obj.put("name",name); // 设备名称
//            obj.put("version",topMoType); // 设备类型
            obj.put(levelValue); // 告警级别
            obj.put(alarmContext); // 告警描述
            data.put(obj);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("table",data);
        /*JSONObject ject1 = new JSONObject();
//        ject1.put("name","设备名称");
//        ject1.put("width","100px");
//        JSONObject ject2 = new JSONObject();
////        ject2.put("name","设备类型");
////        ject2.put("width","100px");
        JSONObject ject3 = new JSONObject();
        ject3.put("name","告警级别");
        ject3.put("width","150px");
        JSONObject ject4 = new JSONObject();
        ject4.put("name","告警描述");
        ject4.put("width","946px");
        JSONArray arr =new JSONArray();
//        arr.put(ject1);
//        arr.put(ject2);
        arr.put(ject3);
        arr.put(ject4);
        jsonObject.put("title","当前警告信息");
        jsonObject.put("thead",arr);
        jsonObject.put("data",data);*/
        return jsonObject.toString();
    }

    /**
     * @Description 获取level
     *
     * @auther: lwj
     * @date: 2018/10/30 0030 下午 4:57
     */
    public String getLevel(JSONArray properties){
        String str = "";
        for (int i = 0; i < properties.length(); i++) {
            JSONObject obj  = (JSONObject) properties.get(i);
            if("level".equals(obj.getString("name"))){
                Object value = obj.getJSONObject("value").get("value");
                if (value instanceof Integer) {
                    str = String.valueOf(value);
                }
            }
        }
        return str;
    }

    /**
     * @Description 获取AlarmContext
     * @auther: lwj
     * @date: 2018/10/30 0030 下午 4:57
     */
    public String getAlarmContext(JSONArray properties){
        String str = "";
        for (int i = 0; i < properties.length(); i++) {
            JSONObject obj  = (JSONObject) properties.get(i);
            if("alarmContext".equals(obj.getString("name"))){
                Object value = obj.getJSONObject("value").get("value");
                if (value instanceof String) {
                    str = String.valueOf(value);
                }
            }
        }
        return str;
    }

    /**
     * @Description 获取TopMoType
     * @auther: lwj
     * @date: 2018/10/30 0030 下午 4:57
     */
    public String getTopMoType(JSONArray properties){
        String str = "";
        for (int i = 0; i < properties.length(); i++) {
            JSONObject obj  = (JSONObject) properties.get(i);
            if("topMoType".equals(obj.getString("name"))){
                Object value = obj.getJSONObject("value").get("value");
                if (value instanceof String) {
                    str = String.valueOf(value);
                }
            }
        }
        return str;
    }
}
